package com.djzhou.gmms.dao.message.impl;

import java.util.Date;

import com.djzhou.gmms.dao.base.impl.BaseDAOImpl;
import com.djzhou.gmms.model.message.TbMessage;
import com.djzhou.gmms.model.user.TbUser;
import com.djzhou.gmms.dao.message.MessageDAO;

public class MessageDAOImpl extends BaseDAOImpl<TbMessage> implements MessageDAO{

	@Override
	public void addSendMessage(Integer senderId, String receiverIds,
			String messageContent) {
		if(receiverIds.indexOf(",")!=-1){
			String[] array=receiverIds.split(",");
			for(String receiverId:array){
				TbMessage message=new TbMessage();
				message.setMessageContent(messageContent);
				message.setSender(getHibernateTemplate().get(TbUser.class, senderId));
				message.setReceiver(getHibernateTemplate().get(TbUser.class, Integer.parseInt(receiverId)));
				message.setMessageState(0);
				message.setMessageTip(0);
				message.setSendDate(new Date());
				message.setSendDelete(0);
				message.setReceiveDelete(0);
				getHibernateTemplate().save(message);
			}
		}else{
			TbMessage message=new TbMessage();
			message.setMessageContent(messageContent);
			message.setSender(getHibernateTemplate().get(TbUser.class, senderId));
			message.setReceiver(getHibernateTemplate().get(TbUser.class, Integer.parseInt(receiverIds)));
			message.setMessageState(0);
			message.setMessageTip(0);
			message.setSendDate(new Date());
			message.setSendDelete(0);
			message.setReceiveDelete(0);
			getHibernateTemplate().save(message);
			
		}		
	}

}
